/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hhx.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hhx.entity.HhxFinance;
import com.thinkgem.jeesite.modules.hhx.entity.HhxOrder;
import com.thinkgem.jeesite.modules.hhx.service.HhxFinanceService;
import com.thinkgem.jeesite.modules.hhx.service.HhxOrderService;
import com.thinkgem.jeesite.modules.hhx.service.HhxUserService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/hhx/hhxOrder")
public class HhxOrderController extends BaseController {

	@Autowired
	private HhxOrderService hhxOrderService;
	@Autowired
	private HhxFinanceService hhxFinanceService;
	
	@Autowired
	private HhxUserService hhxUserService;
	
	@ModelAttribute
	public HhxOrder get(@RequestParam(required=false) String id) {
		HhxOrder entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hhxOrderService.get(id);
		}
		if (entity == null){
			entity = new HhxOrder();
		}
		return entity;
	}
	
	@RequiresPermissions("hhx:hhxOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(HhxOrder hhxOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HhxOrder> page = hhxOrderService.findPage(new Page<HhxOrder>(request, response), hhxOrder); 
		model.addAttribute("page", page);
		return "modules/hhx/hhxOrderList";
	}

	@RequiresPermissions("hhx:hhxOrder:view")
	@RequestMapping(value = "form")
	public String form(HhxOrder hhxOrder, Model model) {
		model.addAttribute("hhxOrder", hhxOrder);
		return "modules/hhx/hhxOrderForm";
	}

	@RequiresPermissions("hhx:hhxOrder:edit")
	@RequestMapping(value = "save")
	public String save(HhxOrder hhxOrder, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hhxOrder)){
			return form(hhxOrder, model);
		}
		hhxOrderService.save(hhxOrder);
		addMessage(redirectAttributes, "保存投资订单成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxOrder/?repage";
	}
	
	@RequiresPermissions("hhx:hhxOrder:edit")
	@RequestMapping(value = "delete")
	public String delete(HhxOrder hhxOrder, RedirectAttributes redirectAttributes) {
		hhxOrderService.delete(hhxOrder);
		addMessage(redirectAttributes, "删除投资订单成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxOrder/?repage";
	}
	
	@RequestMapping(value= {"wxsave",""})
	public String wxsave(HttpServletRequest request, HttpServletResponse response, String userid,String financeid,String amt,String financedesc) {
		String id = UUID.randomUUID().toString().replace("-", "");
		int result = hhxOrderService.insertOrder(id,userid,financeid,amt,financedesc);
		Map<String, String> map = new HashMap<String, String>();
		if (result>0) {
			map.put("result","true");
			map.put("msg","保存成功");
		}else {
			map.put("result","false");
			map.put("msg","保存失败");
		}
		return renderString(response, map);
	}
	
	@RequestMapping(value = {"wxlist", ""})
	public String wxlist(HhxOrder hhxOrder, HttpServletRequest request, HttpServletResponse response, String userid) {
		List<Object[]> orderlist = hhxOrderService.getList(userid);
		return renderString(response, orderlist);
	}
	
	@RequestMapping(value= {"wxupdate",""})
	public String wxupdate(HttpServletRequest request, HttpServletResponse response, String id,String status,String financeid,String userid) {
		
		int result = hhxOrderService.updateOrder(id,status);
		Map<String, String> map = new HashMap<String, String>();
		if (result>0) {
			hhxFinanceService.updateamt(financeid);
			hhxUserService.updateamt(userid);
			map.put("result","true");
			map.put("msg","保存成功");
		}else {
			map.put("result","false");
			map.put("msg","保存失败");
		}
		return renderString(response, map);
	}

}